# -*- coding: utf-8 -*-

from autoscrapy.db.mongo_db import mongoClient
from autoscrapy.utils import log


LOG = log.get_logging(__name__)


class item(object):
    DATABASE = 'autoscrapy'
    COLLECTION = 'articles'

    def __init__(self, context):
        self._context = context
        self._client = mongoClient(self.DATABASE)


    def add_item(self, content):
        if not content or not content['title'] or (len(content['content']) < 30):
            return

        item = {
            'collection': self.COLLECTION,
            'title': content['title'],
            'content': content['content'],
            'images': content['images'],
            'fromsrc': content['fromsrc']
        }
        try:
            self._client.add_entrys([item])
        except Exception as e:
            LOG.error("add item %s fail", content)
            LOG.exception("add item occure exception %s", e)

